<?php

namespace App\Console\Commands;

use App\Api\Account\Models\ChatMessage;
use App\Api\Account\Models\SystemMessage;
use App\Api\Account\Models\SystemMessageUser;
use App\Api\Account\Models\UserVip;
use App\Api\Account\Services\BusinessService;
use App\Api\Sport\Models\CouponCodes;
use App\Api\Sport\Models\CpResultsFinal;
use App\Api\Sport\Models\Cps;
use App\Api\Sport\Models\GroupTeam;
use App\Api\Sport\Models\Players;
use App\Api\Sport\Models\SignUpOrders;
use App\Api\Sport\Services\SportService;
use App\Api\Web\Models\House;
use App\Api\Web\Models\OrderHouse;
use App\Libs\AiText\XunfeiAi;
use App\Libs\Wechat\WechatService;
use App\User;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Foundation\Inspiring;
use OSS\Core\OssException;
use OSS\OssClient;
use Qiniu\Auth;
use Qiniu\Storage\BucketManager;

class OrderCancelCheckCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'order:cancel-check';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Display an inspiring quote';

    /**
     * Execute the console command.
     *
     * @return mixed
     */

    public function handle()
    {
        info('s');
        $os = OrderHouse::where('status', '<', 400)
            ->get();
        $now = Carbon::now()->toDateTimeString();
        foreach ($os as $o) {
            $m = $o;
            $status = data_get($o, 'status', '');
            $dingPaymentAt = data_get($m, 'ding_payment_at', '');
            $sure_rent_at = data_get($m, 'sure_rent_at', '');
            $tmptenementUserId = data_get($m, 'tenement_user_id', '');
            $tmplandlordUserId = data_get($m, 'landlord_user_id', '');
            $tmpstatus = data_get($m, 'status', '');
            $ding_total = data_get($m, 'ding_total', '');
            $pay_total = data_get($m, 'pay_total', '');
            $ding_integral = data_get($m, 'ding_integral', '');
            $mm = clone $m;
            switch ($status) {
                // 只要支付房租倒计时到了 定金就自动划转给房东。
                case '100':
                    if (Carbon::parse($dingPaymentAt)->addDays(5)->toDateTimeString() < $now) {
                        $m->update([
                            'status' => '-200',
                            'tenement_status' => '-200',
                            'landlord_status' => '-200',
                        ]);
                        BusinessService::freezeTransaction(data_get($mm, 'landlord_user_id', 0), -$pay_total, data_get($mm, 'id', ''), 'order_house', data_get($mm, 'house_titile', '').'定金解冻');
                        BusinessService::commonTransaction(data_get($mm, 'landlord_user_id', 0), $pay_total, data_get($mm, 'id', ''), 'order_house', data_get($mm, 'house_titile', '').'定金解冻');
                        SystemMessageUser::create([
                            'user_id' => data_get($mm, 'landlord_user_id', 0),
                            'type' => 2,
                            'message' => data_get($mm, 'house_titile', '') . '订单已被取消',
                        ]);
                        SystemMessageUser::create([
                            'user_id' => data_get($mm, 'tenement_user_id', 0),
                            'type' => 2,
                            'message' => data_get($mm, 'house_titile', '') . '订单已被取消',
                        ]);
                        toWxMessage($tmptenementUserId, data_get($mm, 'landlord_user_id', 0), data_get($mm, 'house_titile', '') . '订单已被取消', '通知');
                        toWxMessage(User::find($tmplandlordUserId), data_get($mm, 'tenement_user_id', 0), data_get($mm, 'house_titile', '') . '订单已被取消', '通知');
                    }
                    break;
                case '200':
                case '300':
                    // 超时定金不退
                    if (Carbon::parse($sure_rent_at)->addDays(4)->toDateTimeString() < $now) {
                        $m->update([
                            'status' => '-200',
                            'tenement_status' => '-200',
                            'landlord_status' => '-200',
                        ]);
                        BusinessService::freezeTransaction(data_get($mm, 'landlord_user_id', 0), -$pay_total, data_get($mm, 'id', ''), 'order_house', data_get($mm, 'house_titile', '').'定金解冻');
                        BusinessService::commonTransaction(data_get($mm, 'landlord_user_id', 0), $pay_total, data_get($mm, 'id', ''), 'order_house', data_get($mm, 'house_titile', '').'定金解冻');

                        SystemMessageUser::create([
                            'user_id' => data_get($mm, 'landlord_user_id', 0),
                            'type' => 2,
                            'message' => data_get($mm, 'house_titile', '').'订单已被取消',
                        ]);
                        SystemMessageUser::create([
                            'user_id' => $tmptenementUserId,
                            'type' => 2,
                            'message' => data_get($mm, 'house_titile', '').'订单已被取消',
                        ]);
                        toWxMessage(User::find($tmptenementUserId), data_get($mm, 'landlord_user_id', 0), data_get($mm, 'house_titile', '').'订单未付款已被取消', '通知');
                        toWxMessage(User::find($tmplandlordUserId), $tmptenementUserId, data_get($mm, 'house_titile', '').'订单未付款已被取消', '通知');
                    }
            }
        }



        // vip状态更新

        $userVips = UserVip::where('start', '<=', $now)
            ->where('end', '>', $now)
            ->get();

        foreach ($userVips as $userVip) {
            BusinessService::refreshVip(User::find(data_get($userVip, 'user_id')));
        }


//        $userIds = [];
//        $hs = \DB::table('users as u')
//            ->join('house as h', 'h.user_id', '=', 'u.id')
//            ->where('u.vip_level_int', '>', 0)
//            ->where('h.status', '>', -900)
//            ->select('h.id', 'h.user_id')
//            ->orderBy('u.vip_level_int', 'desc')
//            ->get();
//
//        $order = 10000;
//        foreach ($hs as $h) {
//            $userId = data_get($h, 'user_id', '');
//            $id = data_get($h, 'id', '');
//            if (!in_array($userId, $userIds)) {
//                array_push($userIds, $userId);
//                $order -= 10;
//                House::find($id)->update([
//                    'order' => $order,
//                ]);
//            }
//        }
//
//        $hs = \DB::table('users as u')
//            ->join('house as h', 'h.user_id', '=', 'u.id')
//            ->where('u.vip_level_int', '=', 0)
//            ->where('h.order', '>', 0)
////            ->where('h.status', '>', -900)
//            ->select('h.id', 'h.user_id')
//            ->orderBy('u.vip_level_int', 'desc')
//            ->get();
//
//        foreach ($hs as $h) {
//            $id = data_get($h, 'id', '');
//                House::find($id)->update([
//                    'order' => 0,
//                ]);
//        }
    }
}
